<!-- 结算应付查询：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" @clickNumber1="clickNumber" @currentRow="currentRow" />
    <Find :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" @clickNumber1="clickNumber2" />

    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 70%">
        <h1 class="f16">结算应付汇总查询</h1>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          selection
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
          :summaryData="summaryData"
          summary
        />
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <PageModule
            :PageData="{ total, pageSizeShow: true, buttonS: true }"
            :layout="'prev, pager'"
            @pageSizeChange="handleSizeChange"
            @pageChange="handleCurrentChange"
          />
        </el-card>
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">结算应付明细查询</h1>
        <Table
          :tableHeader="tableHeader2"
          :tableData="tableData2"
          selection
          class="mt10"
          :isLoading="isLoading2"
          :summaryData="summaryData2"
          summary
        />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { selectList } from '@/plugins/mixin'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'SettlementPayableQueryKl'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'

const formList = ref<any>([
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    dateType: 'month',
    type: 'dateMonth',
    dateLabel: '结算月份',
    dateValue: ''
  },
  {
    type: 'radio1',
    checkboxLabel: '未对账',
    label: '1'
  },
  {
    type: 'radio1',
    checkboxLabel: '部分对账',
    label: '2'
  },
  {
    type: 'radio1',
    checkboxLabel: '已对账',
    label: '3'
  },
  {
    type: 'radio1',
    checkboxLabel: '全部',
    label: '0'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '更新对账数据'
  }
])
const formList2 = ref<any>([
  {
    type: 'input',
    inputPlaceholder: '单据号码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '结算单号',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '单据日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '应付类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '结算月份',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '费用类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '结算方式',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '结算日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '指令号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '送货单号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'radio1',
    checkboxLabel: '未结算',
    label: '1'
  },
  {
    type: 'radio1',
    checkboxLabel: '已结算',
    label: '2'
  },
  {
    type: 'radio1',
    checkboxLabel: '全部',
    label: '0'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '更新对账数据') {
    const res = (await LoginWork.get('/AnnualPayable/updateStatistics')) as any
    if (res.code === 200) {
      JMessage.success('更新成功')
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 3,
    num: 0
  },
  {
    index: 4,
    num: 0
  },
  {
    index: 5,
    num: 0
  },
  {
    index: 6,
    num: 0
  },
  {
    index: 7,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
const flag = ref(3)
const clickNumber = (val: any) => {
  switch (val) {
    case '0': {
      flag.value = 3
      break
    }
    case '3': {
      flag.value = 2
      break
    }
    case '2': {
      flag.value = 1
      break
    }
    case '1': {
      flag.value = 0
      break
    }
  }
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/SettlementPayable/getSettlementSummary', {
    vendorName: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue?.split(' ')[0],
    vendorNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue?.split(' ')[1],
    financeYymm: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
    )?.dateValue,
    checkFlag: flag.value,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
  summaryData.value[0].num = tableDataList.amt
  summaryData.value[1].num = tableDataList.balanceAmt
  summaryData.value[2].num = tableDataList.unBalanceAmt
  summaryData.value[3].num = tableDataList.apAmt
  summaryData.value[4].num = tableDataList.unApAmt
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const tableHeader = ref([
  {
    label: '厂商',
    prop: 'vendorName'
  },
  {
    label: '结算月份',
    prop: 'financeYymm'
  },
  {
    label: '总金额',
    prop: 'amt'
  },
  {
    label: '已对账金额',
    prop: 'balanceAmt'
  },
  {
    label: '未对账金额',
    prop: 'unBalanceAmt'
  },
  {
    label: '已付金额',
    prop: 'apAmt'
  },
  {
    label: '未付金额',
    prop: 'unApAmt'
  }
])
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
  getTableList2()
}

// 表二模块
// 表二按钮
const findClicked2 = (content: string) => {
  if (content === '置空') {
    formList2.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData2 = ref([])
const isLoading2 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 16,
    num: 0
  },
  {
    index: 18,
    num: 0
  }
])
// 分页器
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
const flag2 = ref(2)
const clickNumber2 = (val: any) => {
  switch (val) {
    case '0': {
      flag2.value = 2
      break
    }
    case '2': {
      flag2.value = 1
      break
    }
    case '1': {
      flag2.value = 0
      break
    }
  }
  getTableList2()
}
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>('/SettlementPayable/getSettlementDetails', {
    ...currentTableRow.value,
    feeType: formList2.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '应付类型')
      ?.selectValue,
    typeName: formList2.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '费用类型')
      ?.selectValue,
    accountName: formList2.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算方式')
      ?.selectValue,
    vendorName: formList2.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue
      ? formList2.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
          ?.selectValue?.split(' ')[0]
      : currentTableRow.value.vendorName,
    vendorNo: formList2.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue
      ? formList2.value
          .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
          ?.selectValue?.split(' ')[1]
      : currentTableRow.value.vendorNo,
    custom: formList2.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    slipNo: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '单据号码'
    )?.inputValue,
    mtlName: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '材料名称'
    )?.inputValue,
    sendNo: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '送货单号'
    )?.inputValue,
    lotNo: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令号'
    )?.inputValue,
    financeYymm: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
    )?.inputValue
      ? formList2.value.find(
          (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算月份'
        )?.inputValue
      : currentTableRow.value.financeYymm,
    balanceNo: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '结算单号'
    )?.inputValue,
    slipDateEnd: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '单据日期'
    )?.dateValue?.[1],
    slipDateStart: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '单据日期'
    )?.dateValue?.[0],
    balanceDateStart: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '结算日期'
    )?.dateValue?.[0],
    balanceDateEnd: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '结算日期'
    )?.dateValue?.[1],
    pageNum: currentPage2.value,
    pageSize: pageSize2.value,
    flag: flag2.value //	有无欠数(Y/N)	string	body	true
  })
  isLoading2.value = false
  tableData2.value = tableDataList.list
  total2.value = tableDataList.total
  // currentRow2()
  summaryData2.value[0].num = tableDataList.qty
  summaryData2.value[1].num = tableDataList.amt
}, 500)
const tableHeader2 = ref([
  {
    label: '应付类型',
    prop: 'feeType'
  },
  {
    width: '150px',
    label: '单据号码',
    prop: 'slipNo'
  },
  {
    label: '单据日期',
    prop: 'slipDate'
  },
  {
    label: '结算方式',
    prop: 'accountName'
  },
  {
    label: '结算月份',
    prop: 'financeYymm'
  },
  {
    label: '结算单号',
    prop: 'balanceNo'
  },
  {
    label: '结算日期',
    prop: 'balanceDate'
  },
  {
    label: '厂商',
    prop: 'vendorName'
  },
  {
    label: '品牌',
    prop: 'custom',
    width: '55'
  },
  {
    label: '指令号',
    prop: 'lotNo'
  },
  {
    label: '费用类型',
    prop: 'typeName'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称/费用描述',
    prop: 'mtlName',
    width: '200'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    width: '55'
  },
  {
    label: '数量',
    prop: 'qty',
    width: '55'
  },
  {
    label: '单价',
    prop: 'price',
    width: '55'
  },
  {
    label: '金额',
    prop: 'amt'
  },
  {
    label: '送货单号',
    prop: 'sendNo'
  },
  {
    label: '部门',
    prop: 'deptNo'
  },
  {
    label: '仓库备注',
    prop: 'remark'
  },
  {
    label: '财务备注',
    prop: 'financeRecMemo'
  }
])

const getSelectList = async () => {
  await selectList(formList2, () => service.get('/back/order/allOrderCondition'))
  let vendorName = await service.get<any, any>('/back/vendorQuery')
  // 插入厂商代码 厂商名称
  vendorName.forEach((item: any, index: any) => {
    if (item === null) return
    formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectOptions?.push({
        optionKey: `${item.vendorName} ${item.vendorNo}`,
        optionLabel: `${item.vendorName} ${item.vendorNo}`,
        optionValue: `${item.vendorName} ${item.vendorNo}`,
        LeftLabel: `${item.vendorName}`,
        RightLabel: `${item.vendorNo}`
      })
    formList2.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectOptions?.push({
        optionKey: `${item.vendorName} ${item.vendorNo}`,
        optionLabel: `${item.vendorName} ${item.vendorNo}`,
        optionValue: `${item.vendorName} ${item.vendorNo}`,
        LeftLabel: `${item.vendorName}`,
        RightLabel: `${item.vendorNo}`
      })
  })
  formList2.value
    .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '应付类型')
    ?.selectOptions?.push(
      {
        optionKey: `材料`,
        optionLabel: `材料`,
        optionValue: `材料`
      },
      {
        optionKey: `费用`,
        optionLabel: `费用`,
        optionValue: `费用`
      }
    )
  formList2.value
    .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算方式')
    ?.selectOptions?.push(
      {
        optionKey: `荣阳大货`,
        optionLabel: `荣阳大货`,
        optionValue: `荣阳大货`
      },
      {
        optionKey: `荣信`,
        optionLabel: `荣信`,
        optionValue: `荣信`
      }
    )

  let expenseType = formList2.value.find(
    (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '费用类型'
  ) as any
  const res2 = (await service.get('/back/getPcCostType')) as any
  res2.forEach((item: any, index: any) => {
    // if (item === null && !item.type_name && !item.TYPE_NO) return
    expenseType.selectOptions?.push({
      optionKey: index,
      optionValue: `${item.typeName}`,
      optionLabel: `${item.typeName}`
    })
  })
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped></style>
